/* eslint-disable */
import axios from 'axios'
const ACCESS_TOKEN = 'vue_app_access_token'

const createSever = ({ baseURL }) => {
  const service = axios.create({
    baseURL,
    withCredentials: true, // 添加cookie
    timeout: 5000
  })

  service.interceptors.request.use(
    config => {
      config.headers[ACCESS_TOKEN] = ''
      return config
    },
    e => Promise.reject(e)
  )
  return service
}

const service = createSever({
  baseURL: ''
})

const promiseCache = new Map()
export function requestCache(options, timeout = 1000) {
  const { url, method } = options
  const key = `${method}_${url}`
  let promise = promiseCache.get(key)
  if (!promise) {
    promise = service(options)
    promiseCache.set(key, promise)
    setTimeout(() => promiseCache.delete(key), timeout)
  }
  return promise
}

export default service